#include "losu.h"
#include "losu_bytecode.h"
#include "losu_dump.h"
#include "losu_gc.h"
#include "losu_mem.h"
#include "losu_object.h"
#include "losu_syntax.h"
#include "losu_vm.h"

#include "collections.h"
#include "hashmap.h"

/** losu-capi (self) -> self
 * @brief: "constructor of class: THISxkit_MODULEcollections_CLASSHashMap"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_constructor(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    MapHeader *h = MapInit(vm);
    if (h) {
        setCPointer(vm, argv, h);
    }
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self, key: any, value: any) -> self
 * @brief: "插入键值对。如果键已存在，覆盖值"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNCput(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 3) {
        __error_TooFewArguments(vm);
        return 0;
    }
    MapHeader *h = getCPointer(vm, argv);
    MapPut(vm, h, argv + 1, argv + 2);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> number
 * @brief: "返回键值对数量"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNCsize(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    MapHeader *h = getCPointer(vm, argv);
    return arg_returnNum(vm, MapSize(vm, h));
}

/** losu-capi (self, key: any) -> any
 * @brief: "返回键对应的值"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNCget(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    MapHeader *h = getCPointer(vm, argv);
    losu_object target;
    MapGet(vm, h, argv + 1, &target);
    stack_push(vm, target);
    return 1;
}

/** losu-capi (self) -> 
 * @brief: "析构函数"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNC__del__(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    MapHeader *h = getCPointer(vm, argv);
    MapFree(vm, h);
    obj_unitsetstr(vm, *argv, "__cptr__", obj_newfalse(vm));
    return 0;
}


/** losu-capi (self, key: any) -> self
 * @brief: "删除指定键的键值对"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNCremove(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    MapHeader *h = getCPointer(vm, argv);
    MapRemove(vm, h, argv + 1);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self, key: any) -> bool
 * @brief: "检查是否包含指定键"
 */
int32_t THISxkit_MODULEcollections_CLASSHashMap_FUNCexists(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    MapHeader *h = getCPointer(vm, argv);
    return MapExists(vm, h, argv + 1) ? arg_returnTrue(vm) : arg_returnFalse(vm);
}